package com.fh.shop.api.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fh.shop.api.member.mapper.IMemberMapper;
import com.fh.shop.api.mq.config.MQConstant;
import com.fh.shop.api.mq.message.PayMessage;
import com.fh.shop.api.order.mapper.IOrderDetailMapper;
import com.fh.shop.api.order.po.OrderDetail;
import com.fh.shop.api.product.mapper.IProductMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
@Slf4j
public class PayConsumer {

    @Resource
    private IMemberMapper memberMapper;
    @Autowired
    private IOrderDetailMapper orderDetailMapper;
    @Autowired
    private IProductMapper productMapper;

    @RabbitListener(queues = MQConstant.Pay.QUEUE_SCORE)
    public void handleScoreMessage(String msg, Message message, Channel channel) throws IOException {
        //获取一个 标志位
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //给会员加积分
            PayMessage payMessage = JSON.parseObject(msg, PayMessage.class);
            Long memberId = payMessage.getMemberId();
            double score = Math.ceil(payMessage.getTotalPrice().doubleValue());
            memberMapper.updateScore(memberId,score);
            log.info("会员{},增加积分成功：{}",memberId,score);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("增加会员积分{}处理错误,原因{}",msg,e.getMessage());
        } finally {
            //手动 ack
            channel.basicAck(deliveryTag,false);
        }
    }

    @RabbitListener(queues = MQConstant.Pay.QUEUE_SALE)
    public void handleSaleMessage(String msg, Message message, Channel channel) throws IOException {
        //获取一个 标志位
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //给订单对应的商品加销量
            PayMessage payMessage = JSON.parseObject(msg, PayMessage.class);
            String orderId = payMessage.getOrderId();
            QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
            orderDetailQueryWrapper.eq("orderId",orderId);
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(orderDetailQueryWrapper);
            orderDetails.forEach(x -> {
                Long productId = x.getProductId();
                Long count = x.getCount();
                productMapper.updateSale(productId, count);
                log.info("商品{},增加销量成功：{}",productId,count);
            });

        } catch (Exception e) {
            e.printStackTrace();
            log.info("添加订单对应的商品销量{}处理错误,原因{}",msg,e.getMessage());
        } finally {
            //手动 ack
            channel.basicAck(deliveryTag,false);
        }
    }

}
